package com.lsy.weizhi2.fragment;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.text.style.URLSpan;
import android.text.style.UnderlineSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lsy.weizhi2.JsonObject.NoteList;
import com.lsy.weizhi2.R;
import com.lsy.weizhi2.adapter.FragmentUpAdapter;
import com.lsy.weizhi2.config.Constants;
import com.lsy.weizhi2.config.MyInfo;
import com.lsy.weizhi2.mybase.BaseApp;
import com.lsy.weizhi2.third.LocationService;
import com.lsy.weizhi2.utils.MyOkHttpUtils;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by 嗨哟 on 2017/3/13.
 */
public class HomeFragmentUp extends Fragment {
    private Context context;
    private RecyclerView fragment_up_rv;
    private FragmentUpAdapter fragmentUpAdapter;
    private TextView textView;
    private LocationService locationService;//定位服务
    private Gson gson;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.home_fragment_up,container,false);
        fragment_up_rv= (RecyclerView) view.findViewById(R.id.fragment_up_rv);
        textView= (TextView) view.findViewById(R.id.textView);
        AutoUtils.autoSize(view);
        return view;
    }

    public synchronized FragmentUpAdapter getFragmentUpAdapter(){
        return fragmentUpAdapter;
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        context=getActivity();
        gson=new Gson();
        locationService= BaseApp.locationService;
        Log.i("HomeFragmentUp","onActivityCreated");
        LinearLayoutManager manager=new LinearLayoutManager(context);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        fragment_up_rv.setLayoutManager(manager);
        fragmentUpAdapter=new FragmentUpAdapter(context,null);
        fragment_up_rv.setAdapter(fragmentUpAdapter);
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.i("HomeFragmentUp","onStart");
    }

    View first;
    int xx,height;
    private Handler handler=new Handler(Looper.getMainLooper());
    @Override
    public void onResume() {
        super.onResume();
        Log.i("HomeFragmentUp","onResume");
        fragmentUpAdapter.start();
        if(MyInfo.ishomefirst){
            locationService.registerListener(MyInfo.mListener);
            locationService.setLocationOption(locationService.getDefaultLocationClientOption());
            locationService.start();
        }

    }



    @Override
    public void onPause() {
        super.onPause();
        Log.i("HomeFragmentUp","onPause");
        fragmentUpAdapter.stop();
        if(MyInfo.ishomefirst){
            if(locationService!=null){
                Log.i("HomeFragmentUp","Service 关闭");
                locationService.unregisterListener(MyInfo.mListener); //注销掉监听
                locationService.stop(); //停止定位服务
            }
        }
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    public void Ceshi(){
        //spannableStringBuilder 用法详解：
        SpannableString ss = new SpannableString("红色打电话斜体删除线绿色下划线图片:.");
        //用颜色标记文本
        ss.setSpan(new ForegroundColorSpan(Color.RED), 0, 2,
                //setSpan时需要指定的 flag,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE(前后都不包括).
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //用超链接标记文本
        ss.setSpan(new URLSpan("tel:4155551212"), 2, 5,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //用样式标记文本（斜体）
        ss.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 5, 7,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //用删除线标记文本
        ss.setSpan(new StrikethroughSpan(), 7, 10,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //用下划线标记文本
        ss.setSpan(new UnderlineSpan(), 10, 16,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //用颜色标记
        ss.setSpan(new ForegroundColorSpan(Color.GREEN), 10, 13,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //获取Drawable资源
        Drawable d = getResources().getDrawable(R.mipmap.index_nearby2);
        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
        //创建ImageSpan
        ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
        //用ImageSpan替换文本
        ss.setSpan(span, 18, 19, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        textView.setText(ss);
        textView.setMovementMethod(LinkMovementMethod.getInstance()); //实现文本的滚动
    }

}
